package com.yungam.imcs.controller.dimission_chain;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yungam.common.bean.dimension.DimensionHistory;
import com.yungam.common.bean.dimension.MaterialType;
import com.yungam.common.response.RetDTO;
import com.yungam.common.service.DimensionHistoryService;
import com.yungam.common.service.MaterialTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Api(tags = "尺寸链历史数据接口")
@RestController
@RequestMapping("/dimension")
public class DimensionHistoryController {

    @Resource
    private DimensionHistoryService historyService;
    @Resource
    private MaterialTypeService typeService;

    @ApiOperation("获取所有的尺寸链历史数据")
    @GetMapping("/history")
    public RetDTO<?> getDimensionHistory(@ApiParam("材料类型") @RequestParam(required = false) Integer type,
                                         @ApiParam("总尺寸最大值") @RequestParam(required = false) Double max,
                                         @ApiParam("总尺寸最小值") @RequestParam(required = false) Double min) {
        // 查询材料类型对应的名称
        List<MaterialType> types = typeService.list();  // 这里查询全部材料, 包括已经删除的材料, 因为材料删除了, 案例并不会删除
        Map<Integer, String> map = new HashMap<>();
        List<Integer> deleted = new ArrayList<>(); // 已经删除材料的 id
        for (MaterialType mt : types) { // 建立映射
            String name = mt.getTrash() ? "DELETED-" + mt.getName() : mt.getName();
            if (mt.getTrash()) deleted.add(mt.getId());
            map.put(mt.getId(), name);
        }
        QueryWrapper<DimensionHistory> qw = new QueryWrapper<>();
        if (type != null) {
            if (type == 99999) qw.in("type", deleted);  // 查询已删除的材料案例
            else qw.eq("type", type);
        }
        if (min != null) qw.ge("`as`", min);
        if (max != null) qw.le("`as`", max);
        List<DimensionHistory> list = historyService.list(qw);
        // 组装数据
        for (DimensionHistory dh : list) {
            if (map.containsKey(dh.getType())) dh.setTypeName(map.get(dh.getType()));
        }
        return RetDTO.data(list);
    }

    @ApiOperation("删除某一历史案例")
    @DeleteMapping("/history")
    public RetDTO<?> delOneHistoryCase(@ApiParam("id") Integer id) {
        historyService.removeById(id);
        return RetDTO.success();
    }

    @ApiOperation("计算尺寸链信息")
    @GetMapping("/calculate")
    public RetDTO<?> calculate(@ApiParam("尺寸链历史案例实体") DimensionHistory history) {
        Map<String, Double> map = new HashMap<>();
        // 计算 a3, a4
        Double as = history.getAs(), a1 = history.getA1(), a2 = history.getA2(), d = history.getD(),
               a3, a4;
        Integer type = history.getType();
        if ((type & 1) == 0) {
            a3 = a1;
            a4 = a2;
        } else {
            a3 = a4 = (as - a1 - a2) / 2;
        }
        map.put("a3", a3);
        map.put("a4", a4);
        // 计算后保存到 尺寸链『历史数据』中
        //      先判断数据库中是否已经有相同的数据 即 as a1 a2 type d 相同
        QueryWrapper<DimensionHistory> qw = new QueryWrapper<>();
        qw.eq("`as`", as).eq("a1", a1).eq("a2", a2).eq("type", type);
        List<DimensionHistory> list = historyService.list(qw);
        if (list.size() == 0) {
            history.setA3(a3);
            history.setA4(a4);
            history.setCreateTime(new Date());
            historyService.save(history);
        }
        return RetDTO.data(map);
    }

}
